<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<script type="text/javascript">
$(function(){	
	  var timetosaygoodbye = getRelativeDate("${vo.avo.auctionTime}");      
       $('#countdown').timeTo({
          timeTo: timetosaygoodbye,
          displayDays: 1,
          theme: "black",
          displayCaptions: true,
          fontSize: 20,
          captionSize: 1
      },function(){
         $.ajax({
         type:"get",
         url:"${initParam.root}auction/endCountDown.do",      
         data:"boardNo=${vo.avo.boardNo}",
         success:function(jsonData){
            if(jsonData=="endCountDown"){
               alert("경매가 종료되었습니다!");
                var url="${initParam.root}auction/showContent.do?boardNo=${vo.avo.boardNo}";
                  $(location).attr('href',url);
            } 
         }//callback
      });//ajax 
    });  

      function getRelativeDate(days){         
          var date = new Date((new Date("${vo.avo.timePosted}")).getTime() + 60000 /* milisec */ * 60 /* minutes */ * 24 /* hours */ * days /* days */);
           return date;
      };	
	
	var info="";

  	$("#sendMessage").popupWindow({	  
		windowURL:"${initParam.root}member/auth_sendMessageForm.do?receiveMemberId=${vo.mvo.memberId}&receiveMemberNo=${vo.mvo.memberNo}",	
		windowName:"sendMessageWindow",
		width:400,
		height:300
		 
	});
	$("#buyPrice").click(function(){
		info="<input type='text' id='bprice'><input type='button' value='입력' id='bpriceCan'>";
		$("#buyPriceView").html(info);
	});
	$("#buyPriceView").on("click","#bpriceCan",function(){
		if(isNaN($("#bprice").val())){
			alert("숫자로 입력해주세요!");
			$("#bprice").val("");
			$("#bprice").focus();
			return;
		}
		if($("#bprice").val()==""){
			alert("값을 입력해주세요!");
			$("#bprice").val("");
			return;
			}
		if($("#bprice").val()!="${vo.avo.buyPrice}"){
			alert("즉시 구매값과 다릅니다!");
			$("#bprice").val("");
			return;
		}
		if(confirm("즉시판매 하시겠습니까?")==false){
			$("#buyPriceView").html("");
			return;
		}
		$.ajax({
			type:"get",
			url:"${initParam.root}auction/auth_buybid.do",		
			data:"boardTitle=${vo.avo.boardTitle}&boardNo=${vo.avo.boardNo}&memberNo=${sessionScope.mvo.memberNo}&bidPrice="+$("#bprice").val(),
			success:function(jsonData){
				if(jsonData=="end"){
					alert("경매가 종료되었습니다!");
					 var url="${initParam.root}auction/showContent.do?boardNo=${vo.avo.boardNo}";
						$(location).attr('href',url);
				}
			}//callback
		});//ajax
	});
	
	$("#auctionPrice").click(function(){
		info="<input type='text' id='price'><input type='button' value='입력' id='priceSub'>";
		$("#auctionPriceView").html(info);
	});
	$("#addWishList").click(function(){	
		$.ajax({
			type:"post",
			url:"${initParam.root}member/auth_addMyWishList.do",		
			data:"boardNo=${vo.avo.boardNo}&boardTitle=${vo.avo.boardTitle}",
			success:function(data){ 
				if(confirm("찜목록에 "+data.boardTitle+"를(을) 등록하였습니다. 찜목록으로 이동하시겠습니까?")){
					 var url="${initParam.root}member/wishListView.do";
					 $(location).attr('href',url); 
				}else{
					return false;
				}				
			}//callback
		});//ajax
	});
	$("#auctionPriceView").on("click","#priceSub",function(){
		
		if($("#leastPerson").val()=="${sessionScope.mvo.memberId}"){
			alert("본인의 입찰 가격이 제일 낮습니다!");
			$("#auctionPriceView").html("");
			return false;
		} 
		
		
		var p=$("#price").val();
		var p2="${vo.avo.auctionPrice}";
		var p3="${vo.avo.buyPrice}";
		p=Number(p);
		p2=Number(p2);
		p3=Number(p3);
		if(isNaN($("#price").val())){
			alert("숫자로 입력해주세요!");
			$("#price").val("");
			$("#price").focus();
			return;
		}
		if(p==0){
			alert("0초과의 값을 입력해주세요!");
			$("#price").val("");
			return;
		}
		if(p>p2){
			alert("경매시작가 보다 작거나 같아야 합니다.");
			$("#price").val("");
			return;
		}
		if(p<=p3){
			alert("즉시 구매가 보다 크거나 같아야 합니다.");
			$("#auctionPriceView").html("");
			return;
		}
		if(confirm("입찰하시겠습니까?")==false){
			$("#auctionPriceView").html("");
			return;
		}
		$.ajax({
			type:"get",
			url:"${initParam.root}auction/bid.do",
			datatype:"json",  
			data:"sellerId=${vo.mvo.memberId}&boardTitle=${vo.avo.boardTitle}&boardNo=${vo.avo.boardNo}&memberNo=${sessionScope.mvo.memberNo}&bidPrice="+$("#price").val(),
			success:function(jsonData){
				 if(jsonData.minPriceResult=="N"){
					 alert("최소입찰등록가 보다 큽니다!");
						$("#price").val("");
				 }else{
					if(jsonData.result=="O"){
						alert($("#price").val()+"원 등록되었습니다!");
						$("#auctionPriceView").html("");
					 	var url="${initParam.root}auction/showContent.do?boardNo=${vo.avo.boardNo}";
							$(location).attr('href',url);
					}else{
						alert("중복된 가격입니다");
						$("#price").val("");
					}
				}
			}//callback
		});//ajax
	});
	$("#cusDel").click(function(){
		if(confirm("삭제요청 하시겠습니까?")==false){
			alert("취소되었습니다.");
			return
		}	
		 $.ajax({
			type:"get",
			url:"${initParam.root}auction/auth_deleteConfirm.do",
			datatype:"json",  
			data:"boardNo=${vo.avo.boardNo}&memberId=${sessionScope.mvo.memberId}",
			success:function(jsonData){
				if(jsonData=="delCon"){
					alert("삭제요청 되었습니다.");
					var url="${initParam.root}auction/showContent.do?boardNo=${vo.avo.boardNo}";
					$(location).attr('href',url);
				}else{
					alert("이미 삭제 요청 된 게시물 입니다.");
				}
			}//callback
		});//ajax 
	});//click
	$("#updat").click(function(){
		$(location).attr('href',"${initParam.root}auction/auth_auctionUpdateForm.do?boardNo=${vo.avo.boardNo}");
	});
});
</script>
<center>
	<table border="1" cellpadding="5">
		<%-------------------------------------------------------------------------- --%>
		<tr>
			<td colspan="1" align="center">카테고리</td>
			<td colspan="4" align="center">${vo.bcvo.bigClassName} >
				${vo.scvo.smallClassName}</td>
		</tr>
		<%-------------------------------------------------------------------------- --%>
		<tr>
			<td>제목 </td>
			<td colspan="4" align="center">${vo.avo.boardTitle}</td>
		</tr>
		<%-------------------------------------------------------------------------- --%>
		<tr>
			<td>작성자</td>
			<td id=writer>
			<c:choose>
				<c:when test="">
					
				</c:when>
				<c:otherwise>
					${fn:substring(vo.mvo.memberId, 0,2)}<c:forEach begin="1"
					end='${fn:length(vo.mvo.memberId)-2}'>*</c:forEach>
					
				</c:otherwise>
				
			</c:choose>
			</td>

			<td>신용등급</td>
			<td colspan="2">${vo.mvo.grade}</td>
		</tr>
		<%-------------------------------------------------------------------------- --%>
		<tr>
			<td colspan="2" rowspan="3"><img width="300" height="200"
				src="${initParam.root}upload/${vo.avo.attatchedFile}"></td>
			<td>수량</td>
			<td>${vo.avo.quantity}</td>
			<!-- 만료시간 -->
			<td rowspan="3"><c:choose>
					<c:when test="${vo.avo.activeCheck==1}">
						<font color="red"><b>경매종료</b></font>
						<!-- 비활성화 -->
					</c:when>
					<c:otherwise>
			만료시간 : 
			<div id="countdown"></div>
					</c:otherwise>
				</c:choose></td>
		</tr>
		<%-------------------------------------------------------------------------- --%>
		<tr>
			<td>경매시작</td>
			<td>${vo.avo.auctionPrice}</td>
		</tr>
		<%-------------------------------------------------------------------------- --%>
		<tr>
			<td>즉시구매</td>
			<td>${vo.avo.buyPrice}</td>
		</tr>
		<%-------------------------------------------------------------------------- --%>
		<tr>
			<td colspan="2" align="center">입찰내역</td>
			<c:choose>
				<c:when test="${vo.avo.activeCheck==1}">
					<td rowspan="2" colspan="3" align="center"><font color="red"><b>경매가
								종료되었습니다!</b></font> <!-- 비활성화 --></td>
				</c:when>
				<c:otherwise>
					<c:choose>
						<c:when
							test="${sessionScope.mvo.memberNo!=vo.mvo.memberNo&&sessionScope.mvo!=null&&sessionScope.mvo.memberActChk==0&&sessionScope.mvo.grade>0}">
							<td rowspan="2"><input id="addWishList" type="button"
								value="찜하기"></td>
							<td rowspan="2" align="center"><input type="button"
								id="auctionPrice" value="입찰등록">
								<p>
									<span id="auctionPriceView"></span>
									<!-- 판매자 시점 --></td>
							<td rowspan="2" align="center"><input type="button"
								id="buyPrice" value="즉시판매">
								<p>
									<span id="buyPriceView"></span></td>
						</c:when>
						<c:when test="${sessionScope.mvo.memberNo==vo.mvo.memberNo}">
							<td rowspan="2" colspan="2" align="center"><input
								type="button" value="수정" id="updat"> <!-- 구매자 시점 --></td>
							<td rowspan="2" colspan="2" align="center"><input
								type="button" id="cusDel" value="삭제"></td>
						</c:when>
						<c:when test="${sessionScope.mvo.grade==0 }">
							<td rowspan="2" colspan="3" align="center">권한이 없는 회원입니다!</td>
						</c:when>
						<c:otherwise>
							<td rowspan="2" colspan="3" align="center">로그인 후 사용 가능 합니다!
								<!-- 비회원 시점 -->
							</td>
						</c:otherwise>
					</c:choose>
				</c:otherwise>
			</c:choose>

		</tr>
		<tr>
			<td colspan="2" align="center"><c:forEach items="${vo.bdvo}"
					var="bdvo" varStatus="i">
					<c:set var="id" value='${bdvo.sellerId}' />				
					<c:if test="${i.count==1}">						
						<input type="hidden" id="leastPerson" name="leastPerson" value="${id}">
		  	최저가 제시자 : ${fn:substring(id, 0,2)}<c:forEach begin="1"
							end='${ fn:length(id)-2}'>*</c:forEach>
						<br>
					</c:if>
			 ${i.count}.   ${fn:substring(id, 0,2)}<c:forEach begin="1"
						end='${fn:length(id)-2}'>*</c:forEach>  님 제시가 ${bdvo.bidPrice}원 <Br>
				</c:forEach></td>

		</tr>
		<%-------------------------------------------------------------------------- --%>
		<tr>
			<td colspan="5" align="center">${vo.avo.boardContent}</td>
		</tr>
		<%-------------------------------------------------------------------------- --%>
		<tr>
			<c:if test="${sessionScope.mvo.memberNo!=vo.mvo.memberNo}">

			</c:if>
		</tr>
	</table>
	
</center>
